<?php
$rid = array();
// $user = 0;
// $counter = 0;
// foreach ($role_id_datus as $role) {
// 	$rid[$counter] = $role->role_id;
// 	$counter++;
// }
// if (in_array(20, $rid)) {
// 	$user = 1;
// } else if (in_array(19, $rid)) {
// 	$user = 2;
// }

?>
<div class="wrapper wrapper-content animated page-heading fadeInRight">
    <div class="row">
        <div class="col-lg-9">
            <div class="tabs-container">
                <ul class="nav nav-tabs" id="tab-list-2">
                    <li class="links active" id="search-link"><a href="#search" role="tab" data-toggle="tab">Search</a></li>
                </ul>
                <div class="tab-content">
                   <div id="search" class="tab-pane active">
                        <div class="panel-body">
                          <div class=" animate fadeIn">
                              <form id="searchForm_wo" class="form-horizontal">
                                  <div class="ibox-title" style="border:none;"><h5 style="color: #00afef"><i>Search</i></h5>
                                  </div>
                                     <div class="ibox-content">
                                          <div class="form-group"><label class="col-lg-4 control-label">Work Order Type</label>
                                              <div class="col-lg-4">
                                                  <!-- <input type="text" placeholder="" name="search_wo_type" class="form-control"> -->
                                                   <select name="search_wo_type" id="" class="form-control">
                                                      <option value="">--</option>
                                                   <?php foreach($wo_type as $wo): ?>
                                                      <option value="<?php echo $wo->meaning;?>"><?php echo $wo->meaning; ?></option>
                                                    <?php endforeach; ?>
                                                   </select>
                                              </div>
                                          </div>
                                          <div class="form-group">
                                              <label class="col-lg-4 control-label">Account Name</label>
                                              <div class="col-lg-5">
                                                  <input type="text" name="search_wo_acct" class="form-control">
                                              </div>
                                          </div>
                                          <div class="form-group">
                                              <label class="col-lg-4 control-label">Status</label>
                                              <div class="col-lg-5">
                                                  <!-- <input type="text" name="search_wo_stat" class="form-control"> -->
                                                  <select name="search_wo_stat" id="" class="form-control">
                                                      <option value="">--</option>
                                                      <option value="pending">Pending</option>
                                                      <option value="completed">Completed</option>
                                                  </select>
                                                  <!-- <select name="search_wo_stat" id="" class="form-control">
                                                      <option value="">--</option>
                                                    <?php foreach($status as $s): ?>
                                                      <option value="<?php echo $s->meaning; ?>"><?php echo $s->meaning; ?></option>
                                                    <?php endforeach; ?>
                                                  </select> -->
                                              </div>
                                          </div>
                                          <div class="form-group">
                                              <label class="col-lg-4 control-label">Assigned To</label>
                                              <div class="col-lg-5">
                                                  <select name="search_wo_assign" id="" class="form-control">
                                                      <option value="">--</option>
                                                    <?php foreach($oprtr as $op): ?>
                                                      <option value="<?php echo $op->user_id; ?>"><?php echo $op->user_name; ?></option>
                                                    <?php endforeach; ?>
                                                  </select>
                                              </div>
                                          </div>
                                          <div class="form-group">
                                              <label class="col-lg-4 control-label">Target Date</label>
                                              <div class="col-lg-5">
                                                  <input type="search" name="search_wo_tardate" id="dp" class="form-control">
                                              </div>
                                          </div>
                                        <!--   <div class="form-group">
                                              <label class="col-lg-4 control-label">Target Date From</label>
                                              <div style="margin-left: 270px;" class="col-lg-5 input-daterange input-group">
                                                  <input type="search" name="search_wo_tardate" id="" class="form-control">
                                                    <span class="input-group-addon">To</span>
                                                  <input type="search" name="search_wo_tardate" id="" class="form-control">
                                              </div>
                                          </div> -->
                                           <div class="form-group">
                                              <label class="col-lg-4 control-label">Completion Date</label>
                                              <div class="col-lg-5">
                                                  <input type="search" name="search_wo_comdate" id="dp2" class="db form-control">
                                              </div>
                                          </div>
                                           <div class="form-group">
                                              <label class="col-lg-4 control-label"></label>
                                              <div class="col-lg-5">
                                                  <a href="javascript:document.getElementById('searchForm_wo').reset();"  class="btn btn-warning btn-md pull-right m-t-n-xs clear-btn" id="clear_btn"> Clear </a>
                                                  <button id="search-btn" class="btn btn-primary btn-md pull-right m-t-n-xs search-btn btn-margin-right" > Search </button>
                                              </div>
                                          </div>
                                      </div>
                                  </form>
                          </div>

                                  <button id = "adds" class="btn btn-success btn-md m-t-n-xs add-tab-btn" type="button"> Add</button>

                          <div class="" id="searchResult" >
                              <div class="">
                                  <div class="ibox-content custom">
                                       <div class="horizontal-slide">
                                          <div class="table-responsive">
                                            <table data-limit-navigation="5" data-page-size="50" data-filter="#filter" data-page-navigation=".pagination" class="footable table-bordered table-compact" style="width:100%;">
                                                <thead>
                                                    <tr>
                                                        <th data-sort-ignore="true" data-type="alpha" style="padding:8px;">Action</th>
                                                        <th data-sort-ignore="false" data-type="alpha" style="padding:8px;">Maintenance Type</th>
                                                        <th data-sort-ignore="true" data-type="alpha" style="padding:8px;">Customer Name</th>
                                                        <th data-sort-ignore="false" data-type="alpha" style="padding:8px;">Assigned to</th>
                                                        <th data-sort-ignore="false" data-type="alpha" style="padding:8px;">Completed by</th>
                                                        <th data-sort-ignore="false" data-type="alpha" style="padding:8px;">Status</th>
                                                        <th data-sort-ignore="true" data-type="alpha" style="padding:8px;">Target Date</th>
                                                        <th data-sort-ignore="true" data-type="alpha" style="padding:8px;">Completion Date</th>
                                                        <th data-sort-ignore="true" data-type="alpha" style="padding:8px;">Last Meter Reading</th>
                                                        <th data-sort-ignore="true" data-type="alpha" style="padding:8px;">New Meter Reading</th>
                                                    </tr>
                                                </thead>
                                                <tbody id="resultfound" >

                                                </tbody>
                                                <tfoot >
                                                <tr>
                                                    <td colspan="7" style='border: none !important;'>
                                                        <ul class="pagination pull-right"></ul>
                                                    </td>
                                                </tr>
                                            </tfoot>
                                            </table>
                                          </div>
                                       </div>
                                  </div>
                              </div>
                          </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div><!-- END of class row-->
</div>
<input type="hidden" id="count_add" value="0">
<input type="hidden" id="count_edit" value="0">
<input type="hidden" id="count_view" value="0">
<script>
$(document).ready(function(){
        var count_edit  =0;
        var count_view  =0;
        $('#dp').datepicker({
            format: "yyyy-mm-dd",
            todayBtn: "linked",
            clearBtn: true,
            todayHighlight: true
        });
        $('#dp2').datepicker({
            format: "yyyy-mm-dd",
            todayBtn: "linked",
            clearBtn: true,
            todayHighlight: true
        });
        $('#clear_btn').click(function(){
          $('#searchForm_wo')[0].reset();
          $("select").trigger("chosen:updated");
        });
        $('select').chosen({"width":"100%"});
        $('#adds').click(function(){
            $('#notify').text("");
                if($('#count_add').val() == 0){
                    $('#tab-list-2').append('<li id="add-link" class="links"><a id="2nd" data-toggle="tab" href="#add">Add<button id="addX" style="margin-left:30px;" class="close" type="button" title="Remove this page">×</button></a></li>');
                    $('.tab-content').append('<div id="add" class="tab-pane"><div class="panel-body"><div id="change2"><div class="sk-spinner sk-spinner-double-bounce"><div class="sk-double-bounce1"></div><div class="sk-double-bounce2"></div></div></div></div></div>');
                    $("#change2").load("<?php echo $this->session->userdata('forload') . '/add_form'; ?>")
                    $('#count_add').val('1');
                    add_action('show'); search_action('close');
                }
          });


        $("#search-btn").click(function(e) {
            e.preventDefault();
            var trHTML='';
            console.log('hi');
            $('#resultfound').empty();
            $('#searchResult').show();
              $.ajax({
                type: "POST",
                url: "<?php echo base_url($this->session->userdata('forajax') . '/search_work_order'); ?>",
                cache: false,
                dataType: 'json',
                data: $('#searchForm_wo').serialize(),
                success: function(e) {
                    console.log(e);
                    if(e == "No results found."){
                        console.log('error');
                        $('#resultfound').html("<td colspan='10' align='center'><h3>No Result Found</h3></td>");
                        notifymsg("danger","No Result Found");
                    }else{
                       
                        var countData=0;
                        // if(su == 1){
                        $.each(e, function(i, item) {
                          if(e[i].status == 'Completed'){
                            trHTML += '<tr style="line-height:0;padding:2px;"><input id="maintain_id" type="hidden" value="'+ e[i].maintain_id +'"><td align="center" style="text-align:center; padding:2px;"><button style="margin-left:2px;margin-bottom:1px;" class="btn btn-sm btn-outline btn-info btn-xs view" title="View"><i class="fa fa-file-text-o fa-lg"></i></button></td><td style="text-align:left; padding-left:5px;">'+ e[i].meaning +'</td><td style="text-align:left; padding-left:5px;">'+ e[i].name +'</td><td style="text-align:left; padding-left:5px;">'+ e[i].assigned +'</td><td style="text-align:left; padding-left:5px;">'+ e[i].completed +'</td><td style="text-align:left; padding-left:5px;">'+ e[i].status +'</td><td style="text-align:left; padding-left:5px;">'+ e[i].target_date +'</td><td style="text-align:left; padding-left:5px;">'+ e[i].completion_date +'</td><td style="text-align:left; padding-left:5px;">'+ e[i].last_reading +'</td><td style="text-align:left; padding-left:5px;">'+ e[i].new_reading +'</td></tr>';
                            countData++;
                          }else{
                            if((e[i].maintain_code == 'NEW' || e[i].maintain_code == 'REP') && e[i].stat_req == 'REQ_RELEASED'){
                              trHTML += '<tr style="line-height:0;padding:2px;"><input id="maintain_id" type="hidden" value="'+ e[i].maintain_id +'"><td align="center" style="text-align:center; padding:2px;"><button  style="margin-left:5px;margin-bottom:1px;" class="btn btn-sm btn-outline btn-info btn-xs complete" title="Complete"><i class="fa fa-arrow-circle-o-right fa-lg"></i></button><button  style="margin-left:5px;margin-bottom:1px;" class="btn btn-sm btn-outline btn-info btn-xs edit" title="Edit"><i class="fa fa-pencil-square-o fa-lg"></i></button><button style="margin-left:2px;margin-bottom:1px;" class="btn btn-sm btn-outline btn-info btn-xs view" title="View"><i class="fa fa-file-text-o fa-lg"></i></button></td><td style="text-align:left; padding-left:5px;">'+ e[i].meaning +'</td><td style="text-align:left; padding-left:5px;">'+ e[i].name +'</td><td style="text-align:left; padding-left:5px;">'+ e[i].assigned +'</td><td style="text-align:left; padding-left:5px;">'+ e[i].completed +'</td><td style="text-align:left; padding-left:5px;">'+ e[i].status +'</td><td style="text-align:left; padding-left:5px;">'+ e[i].target_date +'</td><td style="text-align:left; padding-left:5px;">'+ e[i].completion_date +'</td><td style="text-align:left; padding-left:5px;">'+ e[i].last_reading +'</td><td style="text-align:left; padding-left:5px;">'+ e[i].new_reading +'</td></tr>';
                              console.log("OK");
                            }else if(!(e[i].maintain_code == 'NEW' || e[i].maintain_code == 'REP')){
                               trHTML += '<tr style="line-height:0;padding:2px;"><input id="maintain_id" type="hidden" value="'+ e[i].maintain_id +'"><td align="center" style="text-align:center; padding:2px;"><button  style="margin-left:5px;margin-bottom:1px;" class="btn btn-sm btn-outline btn-info btn-xs complete" title="Complete"><i class="fa fa-arrow-circle-o-right fa-lg"></i></button><button  style="margin-left:5px;margin-bottom:1px;" class="btn btn-sm btn-outline btn-info btn-xs edit" title="Edit"><i class="fa fa-pencil-square-o fa-lg"></i></button><button style="margin-left:2px;margin-bottom:1px;" class="btn btn-sm btn-outline btn-info btn-xs view" title="View"><i class="fa fa-file-text-o fa-lg"></i></button></td><td style="text-align:left; padding-left:5px;">'+ e[i].meaning +'</td><td style="text-align:left; padding-left:5px;">'+ e[i].name +'</td><td style="text-align:left; padding-left:5px;">'+ e[i].assigned +'</td><td style="text-align:left; padding-left:5px;">'+ e[i].completed +'</td><td style="text-align:left; padding-left:5px;">'+ e[i].status +'</td><td style="text-align:left; padding-left:5px;">'+ e[i].target_date +'</td><td style="text-align:left; padding-left:5px;">'+ e[i].completion_date +'</td><td style="text-align:left; padding-left:5px;">'+ e[i].last_reading +'</td><td style="text-align:left; padding-left:5px;">'+ e[i].new_reading +'</td></tr>';                           
                              console.log("NOT OK");
                            }else{
                               trHTML += '<tr style="line-height:0;padding:2px;"><input id="maintain_id" type="hidden" value="'+ e[i].maintain_id +'"><td align="center" style="text-align:center; padding:2px;"><button  style="margin-left:5px;margin-bottom:1px;" class="btn btn-sm btn-outline btn-info btn-xs edit" title="Edit"><i class="fa fa-pencil-square-o fa-lg"></i></button><button style="margin-left:2px;margin-bottom:1px;" class="btn btn-sm btn-outline btn-info btn-xs view" title="View"><i class="fa fa-file-text-o fa-lg"></i></button></td><td style="text-align:left; padding-left:5px;">'+ e[i].meaning +'</td><td style="text-align:left; padding-left:5px;">'+ e[i].name +'</td><td style="text-align:left; padding-left:5px;">'+ e[i].assigned +'</td><td style="text-align:left; padding-left:5px;">'+ e[i].completed +'</td><td style="text-align:left; padding-left:5px;">'+ e[i].status +'</td><td style="text-align:left; padding-left:5px;">'+ e[i].target_date +'</td><td style="text-align:left; padding-left:5px;">'+ e[i].completion_date +'</td><td style="text-align:left; padding-left:5px;">'+ e[i].last_reading +'</td><td style="text-align:left; padding-left:5px;">'+ e[i].new_reading +'</td></tr>';
                               console.log("WALA");
                            }
                            countData++;
                          }
                        });
                        // }



                        // else if(su == 2){
                        // $.each(e, function(i, item) {
                        //   if(e[i].status == 'Completed'){
                        //     trHTML += '<tr style="line-height:0;padding:2px;"><input id="maintain_id" type="hidden" value="'+ e[i].maintain_id +'"><td style="text-align:center; padding:2px;"><button style="margin-left:2px;margin-bottom:1px;" class="btn btn-sm btn-outline btn-info btn-xs view" title="View"><i class="fa fa-file-text-o fa-lg"></i></button></td><td style="text-align:left; padding-left:5px;">'+ e[i].meaning +'</td><td style="text-align:left; padding-left:5px;">'+ e[i].name +'</td><td style="text-align:left; padding-left:5px;">'+ e[i].assigned +'</td><td style="text-align:left; padding-left:5px;">'+ e[i].completed +'</td><td style="text-align:left; padding-left:5px;">'+ e[i].status +'</td><td style="text-align:left; padding-left:5px;">'+ e[i].target_date +'</td><td style="text-align:left; padding-left:5px;">'+ e[i].completion_date +'</td><td style="text-align:left; padding-left:5px;">'+ e[i].last_reading +'</td><td style="text-align:left; padding-left:5px;">'+ e[i].new_reading +'</td></tr>';
                        //     countData++;
                        //   }else{
                        //     trHTML += '<tr style="line-height:0;padding:2px;"><input id="maintain_id" type="hidden" value="'+ e[i].maintain_id +'"><td style="text-align:center; padding:2px;"><button style="margin-left:2px;margin-bottom:1px;" class="btn btn-sm btn-outline btn-info btn-xs view" title="View"><i class="fa fa-file-text-o fa-lg"></i></button></td><td style="text-align:left; padding-left:5px;">'+ e[i].meaning +'</td><td style="text-align:left; padding-left:5px;">'+ e[i].name +'</td><td style="text-align:left; padding-left:5px;">'+ e[i].assigned +'</td><td style="text-align:left; padding-left:5px;">'+ e[i].completed +'</td><td style="text-align:left; padding-left:5px;">'+ e[i].status +'</td><td style="text-align:left; padding-left:5px;">'+ e[i].target_date +'</td><td style="text-align:left; padding-left:5px;">'+ e[i].completion_date +'</td><td style="text-align:left; padding-left:5px;">'+ e[i].last_reading +'</td><td style="text-align:left; padding-left:5px;">'+ e[i].new_reading +'</td></tr>';
                        //     countData++;
                        //   }
                        // });
                        // }



                        // else{
                        //   $.each(e, function(i, item) {
                        //     trHTML += '<tr style="line-height:0;padding:2px;"><input id="maintain_id" type="hidden" value="'+ e[i].maintain_id +'"><td style="text-align:center; padding:2px;"><button style="margin-left:2px;margin-bottom:1px;" class="btn btn-sm btn-outline btn-info btn-xs view" title="View"><i class="fa fa-file-text-o fa-lg"></i></button></td><td style="text-align:left; padding-left:5px;">'+ e[i].meaning +'</td><td style="text-align:left; padding-left:5px;">'+ e[i].name +'</td><td style="text-align:left; padding-left:5px;">'+ e[i].assigned +'</td><td style="text-align:left; padding-left:5px;">'+ e[i].completed +'</td><td style="text-align:left; padding-left:5px;">'+ e[i].status +'</td><td style="text-align:left; padding-left:5px;">'+ e[i].target_date +'</td><td style="text-align:left; padding-left:5px;">'+ e[i].completion_date +'</td><td style="text-align:left; padding-left:5px;">'+ e[i].last_reading +'</td><td style="text-align:left; padding-left:5px;">'+ e[i].new_reading +'</td></tr>';
                        //     countData++;
                        // });

                        // }
                        $('#resultfound').html(trHTML).trigger('footable_redraw');;
                        notifymsg("success","   . . . Search Success  , Found("+countData+") Item(s)");
                    }
                },
                complete: function(data) {
                      $('#search-btn').removeAttr('disabled');
                },
                beforeSend: function(){
                      $('#search-btn').attr('disabled',true);
                }
              });
            });

        $(document).on('click','.btn.btn-sm.btn-outline.btn-info.btn-xs.edit',function(){
          close_edit();
            var id         = $(this).closest('tr').children('#maintain_id').val();
            if($('#count_edit').val() == 0){
                $('#tab-list-2').append('<li id="edit-link" class="links"><a data-toggle="tab" href="#edit">Edit<button id="editX" style="margin-left:30px;" class="close" type="button" title="Remove this page">×</button></a></li>');
                $('.tab-content').append('<div id="edit" class="tab-pane"><div class="panel-body"><div id="change3"></div></div></div>');
                $("#count_edit").val('1');
               }
            $("#change3").load("<?php echo $this->session->userdata('forload') . '/edit_form/'; ?>"+id+"/edit")
            edit_action('show'); search_action('close');
            notifymsg("hide","hide");
        });
        $(document).on('click','.btn.btn-sm.btn-outline.btn-info.btn-xs.complete',function(){
          close_edit();
            var id         = $(this).closest('tr').children('#maintain_id').val();
            if($('#count_edit').val() == 0){
                $('#tab-list-2').append('<li id="edit-link" class="links"><a data-toggle="tab" href="#edit">Edit<button id="editX" style="margin-left:30px;" class="close" type="button" title="Remove this page">×</button></a></li>');
                $('.tab-content').append('<div id="edit" class="tab-pane"><div class="panel-body"><div id="change3"></div></div></div>');
                $("#count_edit").val('1');
               }
            $("#change3").load("<?php echo $this->session->userdata('forload') . '/edit_form/'; ?>"+id+"/com")
            edit_action('show'); search_action('close');
            notifymsg("hide","hide");
        });
        $(document).on('click','.btn.btn-sm.btn-outline.btn-info.btn-xs.view',function(){
            var id         = $(this).closest('tr').children('#maintain_id').val();
            if($('#count_view').val() == 0){
                $('#tab-list-2').append('<li id="view-link" class="links"><a data-toggle="tab" href="#view">View<button id="viewX" style="margin-left:30px;" class="close" type="button" title="Remove this page">×</button></a></li>');
                $('.tab-content').append('<div id="view" class="tab-pane"><div class="panel-body"><div id="change4"></div></div></div>');
                $("#count_view").val('1');
                }
            $("#change4").load("<?php echo $this->session->userdata('forload') . '/view_form/'; ?>"+id)
            view_action('show'); search_action('close');
            notifymsg("hide","hide");
        });


});
</script>